home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / euphor12 / walkdir.ex < prev    next >
Text File  |  1994-05-12  |  4KB  |  133 lines

  1.     -- "walk" through a directory structure
  2.  
  3. -- example: 
  4. --    ex walkdir c:\
  5. -- will examine your whole C: hard disk
  6.  
  7. -- This program recursively searches a directory and all of its subdirectories.
  8. -- For now we just make a sorted list of the biggest files encountered in 
  9. -- the search. This shows how to use the dir() built-in function.
  10. -- You might want to modify the look_at() routine below to report other 
  11. -- information. 
  12.  
  13. -- usage:  ex walkdir [path-name]  -- defaults to current directory
  14.  
  15. -- Note that you can cd into any directory and say "ex walkdir" since 
  16. -- c:\euphoria\bin should be on your search path.
  17.  
  18. constant YES = 1, NO = 0
  19.  
  20. -------- Select the information you want to see ------------------------------
  21. constant FILE_INFO = YES,          -- display info on each file
  22.      BIGGEST = YES,               -- print a list of the biggest files
  23.      BIG_NUM = 20,           -- number of big files to display
  24.      RUNNING_FILE_COUNT = YES  -- display running count of files
  25. ------------------------------------------------------------------------------
  26.  
  27. include file.e
  28. include sort.e
  29.  
  30. constant SCREEN = 1, ERR = 2
  31. sequence cmd
  32.  
  33. sequence big_list
  34. big_list = {{-1, "dummy"}}
  35.  
  36. constant INDENT_AMOUNT = 4
  37.  
  38. integer indent
  39. indent = 0
  40.  
  41. integer nfiles
  42. nfiles = 0
  43.  
  44. procedure show_biggest_files()
  45. -- print list of biggest files
  46.  
  47.     if big_list[length(big_list)][1] = -1 then
  48.     -- delete dummy entry
  49.     big_list = big_list[1..length(big_list)-1]
  50.     if length(big_list) = 0 then
  51.         return
  52.     end if
  53.     end if
  54.     printf(SCREEN, "\nThe Top %d\n\n", length(big_list))
  55.     for i = 1 to length(big_list) do
  56.     printf(SCREEN, "%7d %-14s\n", big_list[i])
  57.     end for
  58. end procedure
  59.  
  60. procedure look_at(sequence path_name, sequence entry)
  61. -- "look at" a file or directory entry
  62.  
  63.     if FILE_INFO then
  64.         puts(SCREEN, repeat(' ', indent))
  65.         printf(SCREEN, "%-14s%3s %6d  %4d/%02d/%02d  %02d:%02d:%02d\n", entry)
  66.     end if
  67.     if find('d', entry[D_ATTRIBUTES]) then
  68.     return
  69.     end if
  70.     nfiles = nfiles + 1
  71.     if not FILE_INFO and RUNNING_FILE_COUNT then
  72.     printf(SCREEN, "%d \r", nfiles)
  73.     end if
  74.  
  75.     -- should it be added to the big files list?
  76.     for i = 1 to length(big_list) do
  77.     if entry[D_SIZE] > big_list[i][1] then
  78.             -- insert new big file
  79.             big_list = append(big_list[1..i-1], 
  80.                   {entry[D_SIZE], path_name & '\\' & entry[D_NAME]}) &
  81.                    big_list[i..length(big_list)]
  82.         if length(big_list) > BIG_NUM then
  83.         -- drop the smallest file
  84.         big_list = big_list[1..length(big_list)-1]
  85.         end if
  86.         exit
  87.     end if
  88.     end for
  89. end procedure
  90.  
  91. procedure walk_dir(sequence path_name)
  92. -- walk through a directory and its subdirectories 
  93. -- in alphabetical order, "looking" at each file
  94.     object d
  95.  
  96.     d = dir(path_name)
  97.     while find(path_name[length(path_name)], " \\") do
  98.     path_name = path_name[1..length(path_name)-1]
  99.     end while
  100.     if atom(d) then
  101.     puts(ERR, "Couldn't open " & path_name)
  102.     abort(1)
  103.     end if
  104.     d = sort(d)
  105.     for i = 1 to length(d) do
  106.     if not find(d[i][D_NAME], {".", ".."}) then
  107.         look_at(path_name, d[i])
  108.         if find('d', d[i][D_ATTRIBUTES]) then
  109.         indent = indent + INDENT_AMOUNT
  110.             walk_dir(path_name & '\\' & d[i][D_NAME])
  111.         indent = indent - INDENT_AMOUNT
  112.         end if
  113.     end if
  114.     end for
  115. end procedure
  116.  
  117. cmd = command_line()
  118. if length(cmd) = 2 then
  119.     cmd = append(cmd, ".") -- default to current directory
  120. elsif length(cmd) != 3 then
  121.     puts(ERR, "usage: ex walkdir path-name\n")
  122.     abort(1)
  123. end if
  124.  
  125. walk_dir(cmd[3])
  126.  
  127. if BIGGEST and length(dir(cmd[3])) > 1 then
  128.     show_biggest_files()
  129. end if
  130. printf(SCREEN, "%d files scanned\n", nfiles)
  131. without warning
  132.  
  133.